#include <stdio.h>
#include "func_file.h"

char *my_strcat(char *dest, const char *src)
{
    int length;
    char ch = dest[0];
    for (length = 0; ch != '\0'; length++)
    {
        ch = dest[length + 1];
    }
    ch = src[0];
    for (int i = 0; ch != '\0'; length++, i++)
    {
        dest[length] = src[i];
        ch = src[i + 1];
    }
    return dest;
}

char *my_strchr(const char *str, int c)
{

    for (; *str != '\0'; str++)
    {
        if (*str == c)
            return (char *)str;
    }
    return NULL;
}

int my_strcmp(const char *s1, const char *s2)
{
    for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++)
    {
        if (*s1 != *s2)
            return (int)(*s1 - *s2);
    }
    return 0;
}

size_t my_strlen(const char *s)
{
    size_t length;
    for (length = 0; *s != '\0'; length++, s++)
        ;
    return length;
}

char *my_strcpy(char *dest, const char *src)
{
    for (; *src != '\0'; src++, dest++)
    {
        *dest = *src;
    }
    *dest = *src;
    return dest;
}

char *my_strstr(const char *str, const char *substr)
{
    char *p = (char *)str; // 保存主串移动匹配过程的地址
    char *q = (char *)substr;
    while (*str != '\0' && *substr != '\0')
    {
        if (*str == *substr)
        {
            str++;
            substr++;
        }
        else
        {
            p++;
            str = p;
            substr = q;
        }
    }
    if (*substr == '\0')
        return p;
    else
        return NULL;
}
